<?php

namespace App\Http\Controllers\common;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class VerifyController extends Controller
{
    public function postIndex(Request $request){
        $mobile = $request -> input('phone');
        //查询用户表 , 判断用户是否存在
        $res = DB::table('sa_member') -> where('mobile','=',$mobile) -> first();
        if($res){
            $verify = $request -> input('vc');
            $this -> verify($mobile,$verify);
        }else{
            $data['status'] = '-1';
            $data['msg'] = '该号码无效,请联系您的客户经理';
        }
        echo json_encode($data);
    }
    public function verify($mobile,$verify){
        $ch = curl_init();
        // 必要参数
        $apikey = "50ab0eeae01191319124586d8deacbce"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
        $text="【全一科技】您的验证码是".$verify;
        // 发送短信
        $data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
        $json_data = curl_exec($ch);
        //如果curl发生错误，打印出错误
        if(curl_error($ch) != ""){
            echo 'Curl error: ' .curl_error($ch);
        }
        //解析返回结果（json格式字符串）
        $array = json_decode($json_data,true);
        if($array['code'] == 0){
            $res['status'] = '1';
            $res['msg'] = '验证码已发送,请注意查收';
        }else{
            $res['status'] = '-1';
            $res['msg'] = '验证码次数超限,一小时内只能发送三次验证码';
        }
        return $res;
    }
}